package com.htfyun.ft;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.htfyun.ft.showinfo.ShowInfoBean;
import com.htfyun.ft.showinfo.ShowInfoItemEnum;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.UsefulUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewAdapterShowInfoItems extends RecyclerView.Adapter<RecyclerViewAdapterShowInfoItems.NormalViewHolder> {

    private final List<ShowInfoBean> showInfoBeanList;

    private final LayoutInflater mInflater;
    private final Context mContext;

    public RecyclerViewAdapterShowInfoItems(Context context, List<ShowInfoBean> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        showInfoBeanList = list;
    }
    @NonNull
    @Override
    public NormalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.recycler_item_factory_main_show_info, parent, false);
        return new NormalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NormalViewHolder holder, int position) {
        ShowInfoBean bean = showInfoBeanList.get(position);
        freshHolderWithBean(holder, bean);
    }

    @Override
    public void onBindViewHolder(@NonNull NormalViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            return;
        }
        ShowInfoBean bean = (ShowInfoBean) payloads.get(0);

        freshHolderWithBean(holder, bean);

        showInfoBeanList.remove(position);
        showInfoBeanList.add(position, bean);
    }

    @Override
    public int getItemCount() {
        return showInfoBeanList.size();
    }

    public List<ShowInfoBean> getShowInfoBeanList() {
        return showInfoBeanList;
    }

    private void freshHolderWithBean(@NonNull NormalViewHolder holder, ShowInfoBean bean) {
        ShowInfoItemEnum itemsEnum = bean.getTestItemsEnum();
        holder.title.setText(itemsEnum.getName());

        if (bean.getTxt() != null) {
            holder.summary.setText(UsefulUtils.getSpannableStringForResult(mContext, bean.getTxt()));
        }

        holder.summary.setTextColor(bean.getResult().getShowColor());

        holder.itemsEnum = itemsEnum;

    }

    public void notifyBeanChanged(ShowInfoBean infoBean) {
        int pos = -1;
        for (int i = 0; i < showInfoBeanList.size(); i++) {
            ShowInfoBean bean = showInfoBeanList.get(i);
            if (bean.getTestItemsEnum() == infoBean.getTestItemsEnum()) {
                pos = i;
                break;
            }
        }
        if (pos >= 0) {
            notifyItemChanged(pos, infoBean);
        }
    }


    public static final class NormalViewHolder extends RecyclerView.ViewHolder {
        private final TextView title;
        private final TextView summary;

        private ShowInfoItemEnum itemsEnum;

        private NormalViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            summary = (TextView) itemView.findViewById(R.id.summary);
        }

        public ShowInfoItemEnum getItemsEnum() {
            return itemsEnum;
        }
    }
}
